# -*- coding: utf-8 -*-
import scrapy
import time
import json
import math
from tencent.items import TencentItem


class CareersSpider(scrapy.Spider):
    name = 'careers'
    allowed_domains = ['careers.tencent.com']
    # ajax的请求地址,api端口，返回的是标准的json格式数据
    times = int(time.time() * 1000)
    time.sleep(0.05)  # 休眠50MS
    start_urls = [
        'https://careers.tencent.com/tencentcareer/api/post/Query?timestamp={}'
        '&cityId=2&keyword=python&pageIndex=1&pageSize=10&language=zh-cn&area=cn'.format(times)]
    page = 1

    def parse(self, response):
        dict_jobs = json.loads(response.body)
        for item in dict_jobs['Data']['Posts']:
            postid = item['PostId']
            details_url = 'https://careers.tencent.com/tencentcareer/api/post/ByPostId?timestamp={0}&postId={1}' \
                          '&language=zh-cn'.format(self.times, postid)
            print('详情：', details_url)
            yield scrapy.Request(url=details_url, callback=self.parse_jobs)
        count = dict_jobs['Data']['Count']
        num_page = math.ceil(count / 10)
        self.page += 1
        if self.page < num_page:
            next_page_url = 'https://careers.tencent.com/tencentcareer/api/post/Query?timestamp={0}&' \
                            'cityId=2&keyword=python&pageIndex={1}&pageSize=10&' \
                            'language=zh-cn&area=cn'.format(self.times, self.page)
            yield scrapy.Request(url=next_page_url, callback=self.parse)

    def parse_jobs(self, response):
        items = TencentItem()
        dict_jobs = json.loads(response.body)
        items['recruit_post'] = dict_jobs['Data']['RecruitPostName']
        items['bg'] = dict_jobs['Data']['BGName']
        items['location'] = dict_jobs['Data']['LocationName']
        items['category'] = dict_jobs['Data']['CategoryName']
        items['last_update_time'] = dict_jobs['Data']['LastUpdateTime']
        items['responsibility'] = dict_jobs['Data']['Responsibility']
        items['requirement'] = dict_jobs['Data']['Requirement']
        print(items['recruit_post'])
        yield items
